import api from '@/core/Api'

const dictionary = {
  namespaced: true,
  state () {
    return {
      dictionarys: {}
    }
  },
  actions: {
    getDictionarys ({commit}) {
      api.get(window.$urls.getDictionarys())
        .then((res) => {
          if (res.data.code === 0) {
            commit('getDictionarys', res.data.data)
          } else {
            commit('getDictionarys', {})
          }
        }).catch(() => {
          commit('getDictionarys', {})
        })
    }
  },
  mutations: {
    /**
     * 设置字典缓存
     */
    getDictionarys (state, data) {
      let dictionarys = {}
      // 设置字典缓存
      for (let key in data) {
        let item = data[key]
        if (!dictionarys[item.dictionaryType.dataCode]) dictionarys[item.dictionaryType.dataCode] = []
        dictionarys[item.dictionaryType.dataCode].push(item)
      }
      state.dictionarys = dictionarys
    }
  },
  getters: {
    dictionarys: (state) => {
      return state.dictionarys
    }
  }
}

export default dictionary
